<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付地址" prop="paymentAddress">
        <el-input
          v-model="queryParams.paymentAddress"
          placeholder="请输入支付地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易哈希" prop="txHash">
        <el-input
          v-model="queryParams.txHash"
          placeholder="请输入交易哈希"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="币种符号" prop="tokenSymbol">
        <el-input
          v-model="queryParams.tokenSymbol"
          placeholder="请输入币种符号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网络类型" prop="network">
        <el-input
          v-model="queryParams.network"
          placeholder="请输入网络类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换的bluebera数量" prop="blueberaAmount">
        <el-input
          v-model="queryParams.blueberaAmount"
          placeholder="请输入兑换的bluebera数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换bluebera汇率" prop="rateToBluebera">
        <el-input
          v-model="queryParams.rateToBluebera"
          placeholder="请输入兑换bluebera汇率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入块时间">
        <el-date-picker
          v-model="daterangeBlockTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="链上状态" prop="scanStatus">
        <el-input
          v-model="queryParams.scanStatus"
          placeholder="请输入链上状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区块高度" prop="blockNumber">
        <el-input
          v-model="queryParams.blockNumber"
          placeholder="请输入区块高度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="gas使用量" prop="gasUsed">
        <el-input
          v-model="queryParams.gasUsed"
          placeholder="请输入gas使用量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认数" prop="confirmations">
        <el-input
          v-model="queryParams.confirmations"
          placeholder="请输入确认数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫描时间" prop="scannedAt">
        <el-date-picker clearable
          v-model="queryParams.scannedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择扫描时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bluebera:logs:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bluebera:logs:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bluebera:logs:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bluebera:logs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="支付地址" align="center" prop="paymentAddress" />
      <el-table-column label="交易哈希" align="center" prop="txHash" />
      <el-table-column label="币种符号" align="center" prop="tokenSymbol" />
      <el-table-column label="网络类型" align="center" prop="network" />
      <el-table-column label="数量" align="center" prop="amount" />
      <el-table-column label="兑换的bluebera数量" align="center" prop="blueberaAmount" />
      <el-table-column label="兑换bluebera汇率" align="center" prop="rateToBluebera" />
      <el-table-column label="入块时间" align="center" prop="blockTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.blockTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="链上状态" align="center" prop="scanStatus" />
      <el-table-column label="区块高度" align="center" prop="blockNumber" />
      <el-table-column label="gas使用量" align="center" prop="gasUsed" />
      <el-table-column label="确认数" align="center" prop="confirmations" />
      <el-table-column label="扫描错误信息" align="center" prop="errorMsg" />
      <el-table-column label="扫描时间" align="center" prop="scannedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scannedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bluebera:logs:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bluebera:logs:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改交易扫描日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="支付地址" prop="paymentAddress">
          <el-input v-model="form.paymentAddress" placeholder="请输入支付地址" />
        </el-form-item>
        <el-form-item label="交易哈希" prop="txHash">
          <el-input v-model="form.txHash" placeholder="请输入交易哈希" />
        </el-form-item>
        <el-form-item label="币种符号" prop="tokenSymbol">
          <el-input v-model="form.tokenSymbol" placeholder="请输入币种符号" />
        </el-form-item>
        <el-form-item label="网络类型" prop="network">
          <el-input v-model="form.network" placeholder="请输入网络类型" />
        </el-form-item>
        <el-form-item label="数量" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="兑换的bluebera数量" prop="blueberaAmount">
          <el-input v-model="form.blueberaAmount" placeholder="请输入兑换的bluebera数量" />
        </el-form-item>
        <el-form-item label="兑换bluebera汇率" prop="rateToBluebera">
          <el-input v-model="form.rateToBluebera" placeholder="请输入兑换bluebera汇率" />
        </el-form-item>
        <el-form-item label="入块时间" prop="blockTime">
          <el-date-picker clearable
            v-model="form.blockTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入块时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="链上状态" prop="scanStatus">
          <el-input v-model="form.scanStatus" placeholder="请输入链上状态" />
        </el-form-item>
        <el-form-item label="区块高度" prop="blockNumber">
          <el-input v-model="form.blockNumber" placeholder="请输入区块高度" />
        </el-form-item>
        <el-form-item label="gas使用量" prop="gasUsed">
          <el-input v-model="form.gasUsed" placeholder="请输入gas使用量" />
        </el-form-item>
        <el-form-item label="确认数" prop="confirmations">
          <el-input v-model="form.confirmations" placeholder="请输入确认数" />
        </el-form-item>
        <el-form-item label="扫描错误信息" prop="errorMsg">
          <el-input v-model="form.errorMsg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="扫描时间" prop="scannedAt">
          <el-date-picker clearable
            v-model="form.scannedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择扫描时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLogs, getLogs, delLogs, addLogs, updateLogs } from "@/api/bluebera/logs"

export default {
  name: "Logs",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 交易扫描日志表格数据
      logsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 扫描时间时间范围
      daterangeBlockTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        paymentAddress: null,
        txHash: null,
        tokenSymbol: null,
        network: null,
        amount: null,
        blueberaAmount: null,
        rateToBluebera: null,
        blockTime: null,
        scanStatus: null,
        blockNumber: null,
        gasUsed: null,
        confirmations: null,
        errorMsg: null,
        scannedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询交易扫描日志列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeBlockTime && '' != this.daterangeBlockTime) {
        this.queryParams.params["beginBlockTime"] = this.daterangeBlockTime[0]
        this.queryParams.params["endBlockTime"] = this.daterangeBlockTime[1]
      }
      listLogs(this.queryParams).then(response => {
        this.logsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        paymentAddress: null,
        txHash: null,
        tokenSymbol: null,
        network: null,
        amount: null,
        blueberaAmount: null,
        rateToBluebera: null,
        blockTime: null,
        scanStatus: null,
        blockNumber: null,
        gasUsed: null,
        confirmations: null,
        errorMsg: null,
        scannedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeBlockTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加交易扫描日志"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getLogs(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改交易扫描日志"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLogs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addLogs(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除交易扫描日志编号为"' + ids + '"的数据项？').then(function() {
        return delLogs(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bluebera/logs/export', {
        ...this.queryParams
      }, `logs_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
